#ifndef CHATCLIENT_H
#define CHATCLIENT_H

#include <QLocalSocket>
#include <QString>
#include "message.h"

class ChatClient : public QObject
{
    Q_OBJECT
    
public:
    explicit ChatClient(QObject *parent = nullptr);
    ~ChatClient();
    
    void connectToServer(const QString &serverName = "LocalChatServer");
    void disconnectFromServer();
    void sendMessage(const QString &content);
    void setUsername(const QString &username);
    bool isConnected() const;
    QString username() const;
    
signals:
    void connected();
    void disconnected();
    void errorOccurred(const QString &error);
    void messageReceived(const Message &message);
    
private slots:
    void onConnected();
    void onDisconnected();
    void onReadyRead();
    void onError(QLocalSocket::LocalSocketError error);
    
private:
    QLocalSocket *m_socket;
    QString m_username;
    bool m_usernameSent; // 标记用户名是否已发送
};

#endif // CHATCLIENT_H